import QtQuick 1.0

Rectangle {
    id: mainView
    color: root.constants.defaultBackgroundColor

    /*!
      Very basic helper function to get \a sizeInBytes in a
      formatted string.
    */
    function formattedSizeString(sizeInBytes)
    {
        var size = sizeInBytes / 1000; // To kB
        var sizeMagnitude = "kB";

        var i = 0;

        while (size > 1000 && i < 2) {
            size = size / 1000;
            i++;
        }

        if (i == 1) {
            sizeMagnitude = "MB";
        }
        else if (i == 2) {
            sizeMagnitude = "GB";
        }

        // Display one decimal.
        return size.toFixed(1) + " " + sizeMagnitude;
    }

    /*!
      Returns status text according to \a state.
    */
    function statusText(state)
    {
        var text = "";

        if (state == 1) {
            text = "Copying...";
        }
        else if (state == 2) {
            text = "Failed to backup!";
        }
        else if (state == 3) {
            text = "Cancelled.";
        }
        else if (state == 4) {
            text = "Backup complete!";
        }

        return text;
    }

    Column {
        id: mainViewLayout
        property int itemCount: 4
        property int itemHeight: (height - spacing * (itemCount - 1)) / itemCount

        anchors.fill: parent
        anchors.margins: {
            top: root.constants.defaultMargin
            right: root.constants.defaultMargin
            left: root.constants.defaultMargin
            bottom: root.constants.defaultMargin
        }
        spacing: root.constants.defaultLayoutSpacing


        Label {
            id: generalInformationBox

            property int xOffset: parent.width / 2

            x: -xOffset
            width: parent.width * 1.5
            height: mainViewLayout.itemHeight

            backgroundColor: root.constants.activeColorAccent
            backgroundColorAccent: root.constants.activeColor
            foregroundColor: root.constants.activeColorAccent

            Column {
                x: parent.xOffset + root.constants.defaultMargin
                y: root.constants.defaultMargin
                width: parent.width - parent.xOffset -
                       root.constants.defaultMargin * 2
                height: parent.height - root.constants.defaultMargin * 2
                spacing: root.constants.defaultLayoutSpacing

                Text {
                    width: parent.width
                    height: parent.height / 2
                    color: root.constants.defaultBackgroundColor
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: root.constants.defaultFontPixelSize
                    wrapMode: Text.WordWrap
                    text: (engine.backupProgressState == 0) ?
                              formattedSizeString(engine.sizeOfMarkedFiles).match("0.00 kB") ?
                                "No files selected" :
                                engine.numberOfMarkedFiles + " files " + "(" +
                                formattedSizeString(engine.sizeOfMarkedFiles) + ") selected" :
                              statusText(engine.backupProgressState);
                }

                ProgressIndicator {
                    id: progressIndicator
                    width: parent.width
                    height: parent.height / 2
                    opacity: (engine.backupProgressState != 0) ? 1.0 : 0.0;
                    value: 0

                    Behavior on opacity {
                        NumberAnimation { duration: root.constants.shortAnimationDuration }
                    }

                    Connections {
                        target: engine

                        onProgress: {
                            progressIndicator.value = value;
                        }
                    }
                }
            } // Column
        }

        Label {
            id: selectTargetBox

            property int xOffset: parent.width / 2

            width: parent.width * 1.5
            height: mainViewLayout.itemHeight
            backgroundColor: root.constants.highlightColor
            backgroundColorAccent: root.constants.highlightColorAccent

            Row {
                width: parent.width - parent.xOffset -
                       targetSelectionButton.width
                height: parent.height
                anchors.left: parent.left
                anchors.leftMargin: root.constants.defaultMargin
                spacing: root.constants.defaultLayoutSpacing

                Text {
                    id: stepNumberText1
                    width: 50
                    height: parent.height
                    color: root.constants.defaultBackgroundColor
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: parent.height
                    text: "1"
                }

                Text {
                    width: parent.width - stepNumberText1.width
                    height: parent.height
                    color: root.constants.defaultBackgroundColor
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: root.constants.defaultFontPixelSize
                    wrapMode: Text.WordWrap
                    text: "Target drive selection"
                }
            }

            Button {
                id: targetSelectionButton
                width: root.width / 3
                height: parent.height / 2
                anchors.right: parent.right
                anchors.rightMargin: parent.xOffset +
                                     root.constants.defaultMargin
                anchors.verticalCenter: parent.verticalCenter

                Text {
                    anchors.fill: parent
                    color: root.constants.defaultForegroundColor
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: root.constants.defaultFontPixelSize
                    text: engine.targetDrive.length == 0 ? "Select" : engine.targetDrive;
                }

                onTapped: {
                    haptics.buttonTapFeedback();
                    engine.refreshDrives();
                    driveModel.filterRemovableDrivesOnly();
                    driveSelectionDialog.source = false;
                    driveSelectionDialog.doDisplay();
                }
            }
        }

        Label {
            id: selectFoldersBox

            property int xOffset: parent.width / 2

            x: -xOffset
            width: parent.width * 1.5
            height: mainViewLayout.itemHeight
            backgroundColor: root.constants.highlightColor
            backgroundColorAccent: root.constants.highlightColorAccent
            gradientFromLeftToRight: false

            Row {
                x: parent.xOffset
                width: parent.width - parent.xOffset -
                       fileBrowserLaunchButton.width -
                       root.constants.defaultLayoutSpacing -
                       root.constants.defaultMargin
                height: parent.height
                spacing: root.constants.defaultLayoutSpacing

                Text {
                    id: stepNumberText2
                    width: 50
                    height: parent.height
                    color: root.constants.defaultBackgroundColor
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: parent.height
                    text: "2"
                }

                Text {
                    width: parent.width - stepNumberText2.width
                    height: parent.height
                    color: root.constants.defaultBackgroundColor
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: root.constants.defaultFontPixelSize
                    wrapMode: Text.WordWrap
                    text: "Selection of folders to backup"
                }
            }

            Button {
                id: fileBrowserLaunchButton
                width: root.width / 4
                height: parent.height * 2 / 3
                anchors.right: parent.right
                anchors.rightMargin: root.constants.defaultMargin
                anchors.verticalCenter: parent.verticalCenter
                iconFilename: "arrow_right_icon.png"

                onTapped: {
                    if (!viewSwitcher.running) {
                        viewSwitcher.switchView(fileBrowserView, false);
                    }
                }
            }
        }

        Label {
            id: backupButtonBox

            property int xOffset: parent.width / 2

            width: parent.width * 1.5
            height: mainViewLayout.itemHeight
            backgroundColor: root.constants.highlightColor
            backgroundColorAccent: root.constants.highlightColorAccent

            Row {
                width: parent.width - parent.xOffset
                height: parent.height
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.leftMargin: root.constants.defaultMargin
                anchors.rightMargin: parent.xOffset + root.constants.defaultMargin
                spacing: root.constants.defaultLayoutSpacing * 2

                Text {
                    id: stepNumberText3
                    width: 50
                    height: parent.height
                    color: root.constants.defaultBackgroundColor
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: parent.height
                    text: "3"
                }

                Button {
                    id: backupButton

                    property int progressState: engine.backupProgressState
                    property int numOfFilesSelected: engine.numberOfMarkedFiles
                    property string targetDrive: engine.targetDrive

                    y: (parent.height - height) / 2
                    width: parent.width - stepNumberText3.width -
                           root.constants.defaultMargin * 2 -
                           root.constants.defaultLayoutSpacing
                    height: parent.height * 2 / 3

                    Text {
                        anchors.centerIn: parent
                        color: root.constants.defaultForegroundColor
                        font.pixelSize: root.constants.titleTextSize
                        text: (backupButton.progressState == 1) ? "Cancel" : "Backup!";
                    }

                    onTapped: {
                        if (engine.backupProgressState == 1) {
                            // The current state is 'InProgress'. Try to cancel the
                            // operation.
                            engine.cancelBackup();
                        }
                        else {
                            if (numOfFilesSelected && targetDrive.length) {
                                // Start the backup.
                                engine.startBackup();
                            }
                            else {
                                // No files or no target drive!
                                root.commonDialog.title = "Unable to perform operation";

                                if (!numOfFilesSelected) {
                                    root.commonDialog.information =
                                        "Select files to backup first!";
                                }
                                else {
                                    root.commonDialog.information =
                                        "No target drive selected!";
                                }

                                root.commonDialog.type = 1;
                                root.commonDialog.doDisplay();
                            }
                        }
                    } // onTapped
                } // Button
            } // Row
        } // Label
    }
}
